package com.erpanderp.wam.asset.model.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.asset.model.entity.AssetDetails;
import com.erpanderp.wam.asset.model.entity.AssetRequest;
import com.erpanderp.wam.asset.model.entity.SystemAssets;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.HibernateUtil;

public class AssetRequestDAO {
	
	 public int getLastGeneratedAssetCode(){			
		String queryString="select count(*) from AssetRequest ar ";			
		Long count = null;	 			
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();		
			Query query = session.createQuery(queryString);
			query.setCacheable(false);
			count = (Long) query.uniqueResult();
			session.getTransaction().commit();
		}catch(HibernateException e){
			e.printStackTrace();
		}			
		count = count+1;
		return count.intValue();	
	}
	 
	 public boolean isAssetCodeExistOrNot(String assetReferenceCode) {
	  boolean isAssetCodeExist = false;
	  try{
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();
			String queryString = "from AssetRequest ar where ar.assetReferenceCode=:assetReferenceCode";
			Query query = session.createQuery(queryString).setString(
					"assetReferenceCode", assetReferenceCode);
			query.setCacheable(false);
			List tempList = query.list();
			if (tempList == null || tempList.size() == 0) {
				isAssetCodeExist = true;
			}
			session.close();
			return isAssetCodeExist;
		} catch (Exception e) {
			e.printStackTrace();
			return isAssetCodeExist;
		}
	}
	 
	 public List<SystemAssets> getAllAssets() { 
			List<SystemAssets> allAssets = null;
			try {
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				Query query = session.createQuery("from SystemAssets sa");
				query.setCacheable(false);
				allAssets = (List<SystemAssets>) query.list();
				session.getTransaction().commit();

			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
			return allAssets;
	}
	 
	 public AssetRequest saveOrUpdateAssetRequest(AssetRequest assetRequest) {
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();
			System.out.println("FLUSH MODE: " + session.getFlushMode());
			session.setFlushMode(FlushMode.AUTO);
			
			session.saveOrUpdate(assetRequest);
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return assetRequest;
	}
	 
	/* public boolean saveAssetDetails(List<AssetDetails> assetDetailsList){
		boolean assetDetailesStored = false;
		System.out.println("======sssssss====DAO====="+assetDetailsList.size());
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.AUTO);
				for(AssetDetails assetDetails : assetDetailsList){
					System.out.println("==========1111111111=========DAO=======");
					session.saveOrUpdate(assetDetails);
				}
				session.getTransaction().commit();
				assetDetailesStored = true;
			}catch(Exception e){
				e.printStackTrace();
				return assetDetailesStored;				
			}
			return assetDetailesStored;
		}	*/
	 
	 public AssetRequest getAssetRequestByAssetCode(String assetCode){
		 
		 	AssetRequest assetRequest;
		 	try{
		 		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		 		session.beginTransaction();
		 		session.setFlushMode(FlushMode.AUTO);
		 		String queryString = "from AssetRequest ar where ar.assetReferenceCode =:assetCode";
		 		Query query = session.createQuery(queryString).setString("assetCode", assetCode);
		 		query.setCacheable(false);
		 		List<AssetRequest> tempList = query.list();
				session.getTransaction().commit();				
				if(tempList != null && tempList.size()>0){
					assetRequest = tempList.get(0);
					return assetRequest;
				}else{
					return null;
				}
		 	}catch (Exception e) {
		 		e.printStackTrace();
		 		return null;			
				}		 	
	}
	 
	 public AssetDetails getAssetDetailsByAssetDetailsId(Long assetDetailsId){
		 
		 AssetDetails assetDetails;
		 	try{
		 		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		 		session.beginTransaction();
		 		session.setFlushMode(FlushMode.AUTO);
		 		String queryString = "from AssetDetails ad where ad.assetDetailsId =:assetDetailsId";
		 		Query query = session.createQuery(queryString).setLong("assetDetailsId", assetDetailsId);
		 		query.setCacheable(false);
		 		List<AssetDetails> tempList = query.list();
				session.getTransaction().commit();				
				if(tempList != null && tempList.size()>0){
					assetDetails = tempList.get(0);
					return assetDetails;
				}else{
					return null;
				}
		 	}catch (Exception e) {
		 		e.printStackTrace();
		 		return null;			
			}		 	
	}
	 
	 public AssetDetails updateAssetDetails(AssetDetails assetDetails) {
			try {
				Session session = HibernateUtil.getSessionFactory()
						.getCurrentSession();
				session.getTransaction().begin();
				System.out.println("FLUSH MODE: " + session.getFlushMode());
				session.setFlushMode(FlushMode.AUTO);
				
				session.saveOrUpdate(assetDetails);
				session.getTransaction().commit();
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
			return assetDetails;
		}
	 
	 public List<AssetDetails> getAllAssetDetailsByAssetCode(Long assetRequestId){
			List<AssetDetails> assetDetails = new ArrayList<AssetDetails>();
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				String queryString = "from AssetDetails ad where ad.assetRequest.assetRequestId = :assetRequestId";
				Query query = session.createQuery(queryString);
				query.setCacheable(false).setLong("assetRequestId", assetRequestId);			
				assetDetails =(List<AssetDetails>) query.list();
				session.getTransaction().commit();
				return assetDetails;	
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}		
	}
	 
	 public List<AssetDetails> getAllApprovedAssetDetailsByAssetCode(Long assetRequestId){
			List<AssetDetails> assetDetails = new ArrayList<AssetDetails>();
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				String queryString = "from AssetDetails ad where ad.assetRequest.assetRequestId = :assetRequestId and ad.assetStatus=:assetStatus";
				Query query = session.createQuery(queryString);
				query.setCacheable(false).setLong("assetRequestId", assetRequestId).setString("assetStatus", CommonLookup.STATUS_APPROVED);			
				assetDetails =(List<AssetDetails>) query.list();
				session.getTransaction().commit();
				return assetDetails;	
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}		
	}
	 
	 public List<AssetDetails> getAllAssignedAssets(Long assetRequestId){
			List<AssetDetails> assetDetails = new ArrayList<AssetDetails>();
			String assetStatus="ASSIGNED";
			
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				String queryString = "from AssetDetails ad where ad.assetRequest.assetRequestId = :assetRequestId and ad.assetAssignedStatus=:assetStatus";
				Query query = session.createQuery(queryString);
				query.setCacheable(false).setLong("assetRequestId", assetRequestId).setString("assetStatus", assetStatus);			
				assetDetails =(List<AssetDetails>) query.list();
				session.getTransaction().commit();
				return assetDetails;	
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}		
	}
	 
	 public List<AssetRequest> getAllAssetRequestsByEmployeeId(Long employeeId){
			List<AssetRequest> assetRequesttList = null;
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.AUTO);
			    String queryString = "from AssetRequest ar where ar.employee.employeeId =:employeeId order by ar.requestDate desc";
			    Query query = session.createQuery(queryString).setLong("employeeId", employeeId);
			    assetRequesttList = query.list();
			}catch (Exception e) {
				e.printStackTrace();
				return null;
			}
			return assetRequesttList;		
		}
	 
	 public boolean updateAssetDetails(List<AssetDetails> assetDetailsList){
			boolean assetDetailsSaved = false;
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.AUTO);
				for(AssetDetails assetDetails : assetDetailsList){					
					session.update(assetDetails);
				}
				session.getTransaction().commit();
				assetDetailsSaved = true;
			}catch(Exception e){
				e.printStackTrace();
				return assetDetailsSaved;
				
			}			
			return assetDetailsSaved;
     }
	 
	 public List<AssetRequest> getAllHandoverAssets(String moduleName,String employeeCode,String employeeName){
	    List<AssetRequest> handoverAssets = new ArrayList<AssetRequest>();
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				String queryString = "from AssetRequest ar where ar.status = :status order by ar.requestDate desc";
				
				if(moduleName != null && !moduleName.equals("")){
					queryString += " and ar.moduleName = :moduleName order by ar.requestDate desc";
				}				
				
				if(employeeCode != null && !employeeCode.equals("")){
					queryString += " and ar.employee.employeeCode = :employeeCode order by ar.requestDate desc";
				}
				
				if(employeeName != null && !employeeName.equals("")){
					queryString += " and (ar.employee.firstName = :employeeName or ar.employee.lastName = :employeeName) order by ar.requestDate desc";
				}
				
				Query query = session.createQuery(queryString);
				query.setCacheable(false).setString("status", CommonLookup.STATUS_ACTIVE);
				
				if(moduleName != null && !moduleName.equals("")){
					query.setString("moduleName", moduleName);
				}
				
				if(employeeCode != null && !employeeCode.equals("")){
					query.setString("employeeCode", employeeCode);
				}
				
				if(employeeName != null && !employeeName.equals("")){
					query.setString("employeeName", employeeName);
				}			
				
				handoverAssets =(List<AssetRequest>) query.list();
				session.getTransaction().commit();
				return handoverAssets;	
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}		
		}

	 public List<String> getAllAssetModules() { 
	  List<String> allModules = null;
			try {
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				Query query = session.createQuery("select distinct ar.moduleName from AssetRequest ar ");
				query.setCacheable(false);
				allModules = (List<String>) query.list();
				session.getTransaction().commit();

			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
			return allModules;
	}
	 
	 //DELETE METHODS
	 public AssetDetails deleteAssetDetailsById(AssetDetails assetDetails)
		{
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				session.delete(assetDetails);
				session.getTransaction().commit();
				return assetDetails;
				
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}		
		}
	 
	 //For getting inprocess records for recommender
	 public List<AssetRequest> getAssetRequestsByRecommenderId(Long employeeId, String status) {
			List<AssetRequest> assetRequests = null;
			try {
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				
				String queryString = "from AssetRequest ar where ar.recommendedBy.employeeId = :employeeId ";
				if(status != null && !status.equals(""))
					queryString += " and ar.recommenderStatus = :status order by ar.requestDate desc";
				
				Query query = session.createQuery(queryString);
				query.setCacheable(false).setLong("employeeId",	employeeId);
				if(status != null && !status.equals(""))
					query.setString("status", status);
				
				assetRequests = (List<AssetRequest>) query.list();
				session.getTransaction().commit();
				return assetRequests;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
	}
	 
	//For getting inprocess records for approver
	 public List<AssetRequest> getAssetRequestByApproverId(Long employeeId, String status) {
		List<AssetRequest> assetRequests = null;
		String recomenderStatus=CommonLookup.STATUS_RECOMMENDED;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			
			String queryString = "from AssetRequest ar where ar.approvedBy.employeeId = :employeeId ";
			if(status != null && !status.equals(""))
				queryString += " and ar.approverStatus = :status and ar.recommenderStatus=:recomenderStatus order by ar.requestDate desc";
			
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setLong("employeeId",	employeeId);
			if(status != null && !status.equals(""))
				query.setString("status", status).setString("recomenderStatus", recomenderStatus);
			
			assetRequests = (List<AssetRequest>) query.list();
			session.getTransaction().commit();
			return assetRequests;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	 

	 public List<AssetRequest> getAssetRequestsBySysAdminId(Long employeeId) {
			List<AssetRequest> assetRequests = null;
			try {
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				
				String queryString = "from AssetRequest ar where ar.assigedByEmployee.employeeId = :employeeId order by ar.requestDate desc";
				
				Query query = session.createQuery(queryString);
				query.setCacheable(false).setLong("employeeId",	employeeId);
				
				
				assetRequests = (List<AssetRequest>) query.list();
				session.getTransaction().commit();
				return assetRequests;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
	}
	 
	 public static void main(String args[]){
			System.out.println(new AssetRequestDAO().getAllAssignedAssets(1l));
		
		}
	 

}
